import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils-gongju/storage.js'
const HomePage = () => import('@/views/HomePage.vue')
const LookHouse = () => import('@/views/LookHouse.vue')
const SeekMsg = () => import('@/views/SeekMsg.vue')
const MyName = () => import('@/views/MyName.vue')
const FavoriteHouse = () => import('@/views/FavoriteHouse.vue')

const HouseDetail = () => import('@/views/HouseDetail.vue')
const Layout = () => import('@/views/Layout.vue')
const Login = () => import('@/views/Login.vue')
const Register = () => import('@/views/Register.vue')
const CityList = () => import('@/views/CityList.vue')

Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    component: Layout,
    redirect: '/homepage',
    children: [
      { path: '/homepage', component: HomePage },
      { path: '/lookhouse', component: LookHouse },
      { path: '/seekmsg', component: SeekMsg },
      { path: '/myname', component: MyName }

    ]
  },
  { path: '/housedetail', component: HouseDetail },
  { path: '/login', component: Login },
  { path: '/register', component: Register },
  { path: '/citylist', component: CityList },
  { path: '/favoritehouse', component: FavoriteHouse }
]

const router = new VueRouter({
  routes
})
const whiteList = ['/login', '/register']
router.beforeEach((to, from, next) => {
  const token = getToken()
  if (token) {
    next()
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
